﻿#include "FileLoadDialog.h"
#include <QCheckBox>
#include "CheckBoxToHeaderView.h"

FileLoadDialog::FileLoadDialog(QWidget *parent): QDialog(parent), ui(new Ui::FileLoadDialog)
{
	ui->setupUi(this);
	//窗体设置
	setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
	setWindowTitle("选择文件");
	//初始化table表格
	InitTableView();
	//添加复选框
	//add_checkBox(ui->tableView, 3);
	//输入信息
	LoadInfoToTableview();
	

}
FileLoadDialog::~FileLoadDialog()
{
}

#define TR_SELECT_FILE (QCoreApplication::translate("LoadDatFileDlg", "Select")) // 选择文件
#define TR_TYPE (QCoreApplication::translate("LoadDatFileDlg", "Type")) // 类型
#define TR_FILE_NAME (QCoreApplication::translate("LoadDatFileDlg", "File Name")) // 文件名
#define TR_WELL_NAME (QCoreApplication::translate("LoadDatFileDlg", "Well Name")) // 井名
#define TR_FLOWING_PRESSURE (QCoreApplication::translate("LoadDatFileDlg", "Flowing Pressure")) // 流压
#define TR_PRESSURE (QCoreApplication::translate("LoadDatFileDlg", "Pressure")) // 压力
#define TR_WATER_SATURATION (QCoreApplication::translate("LoadDatFileDlg", "Water Saturation")) // 含水饱和度
void FileLoadDialog::InitTableView()
{
	//20220630 设置列数
	m_table_mode.setColumnCount(3);
	m_table_mode.setHeaderData(0, Qt::Horizontal, "选择");//, Qt::AlignRight
	m_table_mode.setHeaderData(1, Qt::Horizontal, "类型"); //设置表头内容 , Qt::AlignRight
	m_table_mode.setHeaderData(2, Qt::Horizontal, "文件名");//", Qt::AlignRight


	//20220630 设置行数
	//m_table_mode.setRowCount(3);
	//m_table_mode.setHeaderData(0, Qt::Vertical, "记录1");
	//m_table_mode.setHeaderData(1, Qt::Vertical, "记录2");
	//m_table_mode.setHeaderData(2, Qt::Vertical, "记录3");

	//20220630 模型与视图绑定
	ui->tableView->setModel(&m_table_mode);
	ui->tableView->setShowGrid(false);						//20220701 取消网格线
	ui->tableView->setAlternatingRowColors(false);			//20220701 行间相同颜色
	ui->tableView->horizontalHeader()->setStyleSheet("QHeaderView::section {"
		"color: black; padding-left: 4px; border: 1px solid #6c6c6c;""}");

	//20220630 设置隐藏行号	 能够实现 setVisible(false)
	ui->tableView->verticalHeader()->setHidden(true);	

	//20220701 表头添加复选框
	CheckBoxToHeaderView* headerCheckbox = new CheckBoxToHeaderView(Qt::Horizontal, ui->tableView);
	ui->tableView->setHorizontalHeader(headerCheckbox);

	//20220701 设置根据内容表头自适应宽度
	ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

	//20220701 设置表头左对齐 生效 这个要放在上面的复选框语句后面 才会生效
	ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);

	//20220701 设置表头占满整个表格,不合适
	//ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
	


}

void FileLoadDialog::LoadInfoToTableview()
{


	//ui->tableView->setColumnHidden(0, true);				//20220630 本想隐藏插入数据的序号列，但还是把标头1列隐藏
	//m_table_mode.removeRows(0, 2);						//20220630 清空表 2行 ？
	QVector<QString> fileNames
	{
		"流压","pwf.out","流压流压流压流压","D:\Software\INSIM2022\INSIM20220415.exe","饱和度","pwf.out12"
	};
	for (int i = 0; i < 3; i++)
	{

		m_table_mode.setItem(i, 1, new QStandardItem(fileNames[i]));
		m_table_mode.setItem(i, 2, new QStandardItem(fileNames[i+1]));


		QModelIndex index = m_table_mode.index(i, 0);
		const auto centeredCheckBoxWidget = new QWidget();

		const auto checkBox = new QCheckBox();
		checkBox->setChecked(true);

		const auto checkBoxLayout = new QHBoxLayout(centeredCheckBoxWidget);
		checkBoxLayout->addWidget(checkBox);
		checkBoxLayout->setAlignment(Qt::AlignCenter);
		checkBoxLayout->setContentsMargins(0, 0, 0, 0);
		centeredCheckBoxWidget->setLayout(checkBoxLayout);

		ui->tableView->setIndexWidget(index, centeredCheckBoxWidget);
	}

} 
void FileLoadDialog::add_checkBox(QTableView* table, int row)
{
	QWidget* widget = new QWidget(table);
	QCheckBox* checkbox = new QCheckBox();
	QHBoxLayout* hLayout = new QHBoxLayout();

	hLayout->addWidget(checkbox);
	hLayout->setMargin(0);									// 必须添加, 否则CheckBox不能正常显示
	hLayout->setAlignment(checkbox, Qt::AlignCenter);		// 居中显示复选框

	widget->setLayout(hLayout);
	table->setIndexWidget(table->model()->index(row, 0), widget);

}

